package com.scsj.supermarket.view.activity.goodmodel;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scsj.supermarket.R;
import com.scsj.supermarket.adapter.BrowsingHistoryAdapter;
import com.scsj.supermarket.interfaces.GoodsType;
import com.scsj.supermarket.utils.SkipUtils;
import com.scsj.supermarket.view.activity.baseactivitymodel.BaseActivity;
import com.vondear.rxtools.view.dialog.RxDialogSureCancel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 浏览记录
 */
public class BrowsingHistoryActivity extends BaseActivity implements View.OnClickListener {
    private LinearLayout back_ll, empty_ll, edit_ll;
    private TextView edit_tv;
    private Boolean isEdit = false;//是否为编辑状态
    private LinearLayout edit_bar_ll;//编辑状态栏  编辑状态显示 非编辑状态隐藏
    private ImageView select_all;//全选
    private Boolean isSelectAll = false;//是否全选

    private RecyclerView recyclerview;
    private BrowsingHistoryAdapter browsingHistoryAdapter;

    private List<Map<String, String>> list;

    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_browsing_history);
    }

    @Override
    protected void initView() {
        back_ll = findViewById(R.id.back_ll);
        empty_ll = findViewById(R.id.empty_ll);
        edit_ll = findViewById(R.id.edit_ll);
        edit_tv = findViewById(R.id.edit_tv);
        edit_bar_ll = findViewById(R.id.edit_bar_ll);
        edit_bar_ll.setVisibility(View.GONE);
        select_all = findViewById(R.id.select_all);

        recyclerview = findViewById(R.id.recyclerview);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        browsingHistoryAdapter = new BrowsingHistoryAdapter(this);
        recyclerview.setAdapter(browsingHistoryAdapter);
    }

    @Override
    protected void initData() {
        list = new ArrayList<>();
        Map<String, String> map = new HashMap<>();
        map.put("type", "0");
        map.put("date", "今天");
        map.put("isSelectd", "0");
        list.add(map);
        map = new HashMap<>();
        map.put("type", "0");
        map.put("date", "");
        map.put("isSelectd", "0");
        list.add(map);
        map = new HashMap<>();
        map.put("type", "1");
        map.put("date", "12月26");
        map.put("isSelectd", "0");
        list.add(map);
        map = new HashMap<>();
        map.put("type", "2");
        map.put("date", "");
        map.put("isSelectd", "0");
        list.add(map);
        map = new HashMap<>();
        map.put("type", "0");
        map.put("date", "");
        map.put("isSelectd", "0");
        list.add(map);
        map = new HashMap<>();
        map.put("type", "0");
        map.put("date", "12月25");
        map.put("isSelectd", "0");
        list.add(map);
        map = new HashMap<>();
        map.put("type", "0");
        map.put("date", "");
        map.put("isSelectd", "0");
        list.add(map);

        browsingHistoryAdapter.addData(list);

    }

    @Override
    protected void initListener() {
        back_ll.setOnClickListener(this);
        empty_ll.setOnClickListener(this);
        edit_ll.setOnClickListener(this);
        select_all.setOnClickListener(this);

        browsingHistoryAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (isEdit) {
                    Map map = (Map) adapter.getItem(position);
                    if (map.get("isSelectd").equals("0")) {
                        map.put("isSelectd", "1");
                    } else {
                        map.put("isSelectd", "0");
                    }
                    adapter.setData(position, map);
                } else {
                    SkipUtils.toGoodsDetails(mContext, GoodsType.common);
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_ll://返回
                finish();
                break;
            case R.id.empty_ll://清空
                final RxDialogSureCancel rxDialogSureCancel = new RxDialogSureCancel(this);//提示弹窗
                rxDialogSureCancel.setContent("是否清空所有足迹");
                rxDialogSureCancel.setSureListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        rxDialogSureCancel.cancel();
                    }
                });
                rxDialogSureCancel.setCancelListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        rxDialogSureCancel.cancel();
                    }
                });
                rxDialogSureCancel.show();
                break;
            case R.id.edit_ll://编辑
                isEdit = !isEdit;
                browsingHistoryAdapter.setEdit(isEdit);
                if (isEdit) {
                    edit_tv.setText("完成");
                    empty_ll.setVisibility(View.INVISIBLE);
                    edit_bar_ll.setVisibility(View.VISIBLE);
                } else {
                    edit_tv.setText("编辑");
                    empty_ll.setVisibility(View.VISIBLE);
                    edit_bar_ll.setVisibility(View.GONE);
                }
                break;
            case R.id.select_all://选择全部
                isSelectAll = !isSelectAll;
                if (isSelectAll) {
                    select_all.setImageResource(R.mipmap.cart_selected);
                } else {
                    select_all.setImageResource(R.mipmap.cart_unselected);
                }
                for (Map<String, String> map : list) {
                    if (isSelectAll) {
                        map.put("isSelectd", "1");
                    } else {
                        map.put("isSelectd", "0");
                    }
                }
                browsingHistoryAdapter.notifyDataSetChanged();

                break;
            default:
                break;
        }
    }
}
